<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;

class Advertself extends Base
{
    /**
     * 自制广告首页信息
     * @return mixed
     */
    public function index()
    {
        $map = [
            'a.status' => 1,
            'a.type' => 1,
            'b.status' => 1,
        ];
        //分页带条件
        if(request()->isPost()){

            $data = input('post.');

            if($data['code'] == 1){

                $name = $data['username']?:'';
                $map = [
                    'b.nick_name' => array('like',"%".$name."%"),
                ];
                //页面渲染 分页带条件

            }elseif($data['code'] == 2){
                //如果状态没有传过来 默认为1
                if($data['pass']){
                    $map['a.status'] = $data['pass'];
                }

                //时间区间
                if($data['start'] && $data['end']){
                    //把时间转换为uninx时间
                    $start_time = strtotime($data['start']);
                    $end_time = strtotime($data['end']);
                    $map['a.create_time'] = ['between',[$start_time,$end_time]];
                }

                //如果没有选择行业则默认为空
                if($data['industry']){
                    $map['b.job_one'] = $data['industry'];
                }

                //受众年龄
                if($data['small-age'] && $data['big-age']){
                    $map['a.attr_age'] = ['between',[$data['small-age'],$data['big-age']]];
                }

                //如果没有选择省份、城市则默认为空
                if($data['province'] || $data['city']){
                    $map['a.province'] = $data['province'];
                    $map['a.city'] = $data['city'];
                }
            }
        }


        //查询自制广告
        $adv_self = Db::field('a.id as aid,a.uid as auid,a.status,a.type,a.attr_age,a.create_time,b.uid as buid,b.nick_name,b.province,b.city,b.status as bstatus,b.job_one,
                                c.id as cid,c.user_name as cname,d.provinceid,d.province as dprovince,e.cityid,e.city as ecity,f.id as fid,f.name as fname')
                    ->table('ad_list')
                    ->alias('a')
                    ->join('ad_user_info b',' a.uid = b.uid','LEFT')
                    ->join('ad_user c',' b.uid = c.id','LEFT')
                    ->join('ad_provinces d',' b.province = d.provinceid','LEFT')
                    ->join('ad_cities e',' b.city = e.cityid','LEFT')
                    ->join('ad_industry f',' b.job_one = f.id','LEFT')
                    ->where($map)
                    ->paginate(1,false,['query'=>request()->param()]);

        //省份搜索数据
        $province = Db::table('ad_provinces')->select();

        //行业选择
        $industry = Db::field('id,name')->table('ad_industry')->where('pid = 0')->select();

        return $this->fetch('index',[
            'adv_self' => $adv_self,
            'province' => $province,
            'industry' => $industry,
        ]);
    }

    //自制广告修改页
    public function update()
    {
        return $this->fetch();
    }
}